//
//  MTPartyDetailHeaderCell.swift
//  MT
//
//  Created by ooops on 2020/5/26.
//  Copyright © 2020 张强. All rights reserved.
//

import UIKit
import Foundation
import Eureka
import MTCategory

public final class MTPartyDetailTimeRow: MTPartyDetailTimeAddressRow, RowType {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTPartyDetailTimeAddressRow: MTPartyDetailTimeAddressCellRow {
    required public init(tag: String?) {
        super.init(tag: tag)
    }
}

open class MTPartyDetailTimeAddressCell: Cell<String>, CellType {
    
    var model: MTPartyListModel? {
        didSet {
            if let time = model?.detailResult?.startTime {
                if let date = DateManager.date(dateString: time, format: "yyyy-MM-dd HH:mm:ss") {
                    partyTimeLabel.text = "时间：" + DateManager.string(date: date, format: "yyyy年MM月dd日 HH:mm")
                }
            }
            
            if let address = model?.detailResult?.address {
                partyAddressLabel.text = "地点: " + "\(address)"
            }
        }
    }

    public required init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    /// 显示模式
    lazy var partyTimeLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.setContentHuggingPriority(.defaultHigh, for: .horizontal)
        return l
    }()
    
    lazy var partyAddressLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(12)
        l.textColor = UIColor.text.x1d1a1a
        l.numberOfLines = 0
        return l
    }()
    
    lazy var lineView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()
    
    open override func setup() {
        super.setup()
        backgroundColor = UIColor.theme.background
        selectionStyle = .none
        textLabel?.isHidden = true
        detailTextLabel?.isHidden = true
        editingAccessoryType =  .none
        row.disabled = Condition(booleanLiteral: true)
        self.height = { UITableView.automaticDimension }
        
        contentView.addSubview(partyTimeLabel)
        contentView.addSubview(partyAddressLabel)
        contentView.addSubview(lineView)
        
        makeConstraint()
    }
    
    
    
    private func makeConstraint() {
        
        lineView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.right.equalToSuperview().offset(-18)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        partyTimeLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(12)
            make.left.equalToSuperview().offset(18)
            make.right.equalToSuperview().offset(-18)
            //make.width.greaterThanOrEqualTo(30)
        }
        
        partyAddressLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(18)
            make.top.equalTo(partyTimeLabel.snp.bottom).offset(2)
            make.right.equalToSuperview().offset(-18)
            make.bottom.equalToSuperview().offset(-12)
        }
    }

    deinit {
//        datePicker.removeTarget(self, action: nil, for: .allEvents)
    }

    open override func update() {
        super.update()
//        if row.value != nil {
//            contentLabel.text = row.displayValueFor?(row.value)
//            contentLabel.font = PingFangSCSemiboldFont(14)
//            contentLabel.textColor = UIColor.text.x9b9b9b
//        }
    }

    open override func didSelect() {
        super.didSelect()
        row.deselect()
    }

    override open var inputView: UIView? {
        return nil
    }
}

open class MTPartyDetailTimeAddressCellRow: Row<MTPartyDetailTimeAddressCell>, NoValueDisplayTextConformance {

    open var noValueDisplayText: String? = nil

    required public init(tag: String?) {
        super.init(tag: tag)
        displayValueFor = { [unowned self] value in
            guard let val = value else { return nil }
            return val
        }
    }
}



